<template>
  <div>
    <el-input
      placeholder="选择/输入图标"
      :value="value"
      clearable
      @input="$emit('input', $event)"
      @clear="$emit('input', '')"
    >
      <template v-if="value" slot="prepend"><i :class="`iconfont-mct ${value}`"></i></template>
      <el-popover slot="append" v-model="showIconList" placement="bottom-end" width="480" height="320" trigger="click">
        <div class="icons-list">
          <el-row>
            <el-col v-for="(item, index) in icons" :key="index" :span="8">
              <el-button type="text" @click="onIconSelect(item)">
                <div style="display: flex; align-items: center; color: #595959">
                  <i class="iconfont-mct" :class="`mct-${item.font_class}`"></i>
                  <span style="margin-left: 5px">{{ item.name }}</span>
                </div>
              </el-button>
            </el-col>
          </el-row>
        </div>
        <el-button slot="reference">选择</el-button>
      </el-popover>
    </el-input>
  </div>
</template>

<script>
export default {
  name: 'IconFontSelect',
  props: {
    value: { type: String, default: '' },
  },
  data() {
    return {
      icons: require('./iconfont.json').glyphs,
      showIconList: false,
    }
  },
  methods: {
    onIconSelect(icon) {
      this.$emit('input', `mct-${icon.font_class}`)
      this.showIconList = false
    },
  },
}
</script>

<style lang="scss" scoped>
.icons-list {
  width: 460px;
  height: 300px;
  overflow: auto;

  .el-col {
    margin-top: 2px;
  }
}
.iconfont-mct {
  font-size: 20px;
}
</style>
